import { Forward, PlayerInput, PlayerSkin, playerState, PosInfo, ServerMessage } from '../define/GameDefine';
import GameSet from '../define/GameSet';
import { UICommandKey } from '../define/UICommand';
import ViewMgr from '../manager/ViewMgr';
import MapModel from '../model/MapModel';
import { MapUnit } from '../object/MapUnit';
import Utils from '../tool/Utils';
import { Player } from './player';

const { ccclass, property } = cc._decorator;

@ccclass
export default class PlayerView extends cc.Component {
  /**
   * 格子地图
   */
  @property(cc.Sprite)
  show: cc.Sprite = null;

  /**
   * 格子地图
   */
  @property(cc.Label)
  forwardLabel: cc.Label = null;

  @property(sp.Skeleton)
  showAni: sp.Skeleton = null;

  start() {
    ViewMgr.ins.addEventListener(UICommandKey.buildDealEvent, this.onDealOtherBuildEvent, this);
  }

  public player: Player;
  public init(player: Player): void {
    this.player = player;
    //皮肤
    let skinName: string = '';
    switch (this.player.color) {
      case PlayerSkin.Blue:
        skinName = 'blue';
        break;
      case PlayerSkin.Red:
        skinName = 'red';
        break;
    }
    this.showAni.setSkin(skinName);
    this.node.zIndex = 20;
  }

  /**
   * 根据位置信息直接改坐标
   */
  public resetPosByPosInfo(info: PosInfo): void {
    let x: number = info.x * GameSet.girdWidth + GameSet.girdWidth / 2;
    let y: number = info.y * GameSet.girdHeight + GameSet.girdHeight / 2;
    this.node.position = new cc.Vec2(x, y);
  }

  /**
   * 运行指令 每帧
   */
  public runOrder(event: ServerMessage, input: number): void {
    if (!input) return;
    //方向
    let forward: Forward = Utils.getForwardByInput(input);
    //事件类型
    switch (event) {
      case ServerMessage.C2S_PlayerInput:
        if (forward) {
          this.player.resetForward(forward);
          this.changeAni();
        } else {
          if (this.player.state == playerState.Ideal) {
            //其他操作
            this.player.runDeal(input);
            //操作·动画
            this.changeAni(true);
          }
        }
        break;
      case ServerMessage.C2S_PlayerCancel:
        if (forward) {
          this.player.cancelForward(forward);
          this.changeAni();
        } else {
          //解放操作动画
          this.changeAni(false);
        }
        break;
    }
  }

  /**
   * building事件
   */
  private onDealOtherBuildEvent(data): void {
    let phid: string = data.data.hid;
    if (phid != this.player.hid) return;
    let x: number = data.data.x;
    let y: number = data.data.y;
    let mapData: MapUnit = MapModel.ins.getData(x, y);
    mapData.onDeal(this.player);
  }

  private changeAni(deal: boolean = false): void {
    //this.forwardLabel.string = this.player.forward.toString();
    let ani: string = '';
    if (deal) {
      switch (this.player.forward) {
        case Forward.Up:
          ani = 'actback';
          break;
        case Forward.Down:
          ani = 'act';
          break;
        case Forward.Left:
          ani = 'actlift';
          break;
        case Forward.Right:
          ani = 'actright';
          break;
      }
    } else if (this.player.state == playerState.Ideal) {
      switch (this.player.forward) {
        case Forward.Up:
          ani = 'standback';
          break;
        case Forward.Down:
          ani = 'stand';
          break;
        case Forward.Left:
          ani = 'standlift';
          break;
        case Forward.Right:
          ani = 'standright';
          break;
      }
    } else if (this.player.state == playerState.Run) {
      switch (this.player.forward) {
        case Forward.Up:
          ani = 'goback';
          break;
        case Forward.Down:
          ani = 'go';
          break;
        case Forward.Left:
          ani = 'golift';
          break;
        case Forward.Right:
          ani = 'goright';
          break;
      }
    }
    let playingAni: string = this.showAni.animation;
    if (playingAni != ani) {
      this.showAni.setAnimation(0, ani, true);
    }
  }

  /**
   * 每帧步进
   * @param dt
   */
  public tick(): void {
    if (this.player.state == playerState.Run) {
      this.run();
      //检测是不是不跑了
      this.checkEnd();
    }
  }

  /**
   * 跑动方法
   */
  public run(): void {
    let step: number = GameSet.pSpeed;
    switch (this.player.forward) {
      case Forward.Up:
        this.node.y += step;
        break;
      case Forward.Down:
        this.node.y -= step;
        break;
      case Forward.Left:
        this.node.x -= step;
        break;
      case Forward.Right:
        this.node.x += step;
        break;
    }
  }

  /**
   * 检测终点
   */
  public checkEnd(): void {
    let getEnd: boolean = false;
    let deadLine: number;
    switch (this.player.forward) {
      case Forward.Up:
        deadLine = this.player.nextPos.y * GameSet.girdHeight + GameSet.girdHeight / 2;
        if (this.node.y > deadLine) {
          getEnd = true;
        }
        break;
      case Forward.Down:
        deadLine = this.player.nextPos.y * GameSet.girdHeight + GameSet.girdHeight / 2;
        if (this.node.y < deadLine) {
          getEnd = true;
        }
        break;
      case Forward.Left:
        deadLine = this.player.nextPos.x * GameSet.girdWidth + GameSet.girdWidth / 2;
        if (this.node.x < deadLine) {
          getEnd = true;
        }
        break;
      case Forward.Right:
        deadLine = this.player.nextPos.x * GameSet.girdWidth + GameSet.girdWidth / 2;
        if (this.node.x > deadLine) {
          getEnd = true;
        }
        break;
    }
    //到达暂定终点
    if (getEnd) {
      this.player.getEnd();
      if (this.player.ForwardOrder.get(this.player.forward) && this.player.checkForward(this.player.forward)) {
        this.player.resetNextPos();
      } else if (
        this.player.nextForward &&
        this.player.ForwardOrder.get(this.player.nextForward) &&
        this.player.checkForward(this.player.nextForward)
      ) {
        //把自己位置对到正中间
        this.resetPos();
        //第二指令跑起来
        this.player.resetNextForward();
        this.player.resetNextPos();
      } else {
        //把自己位置对到正中间
        this.resetPos();
        //取消了 不跑了
        this.player.stop();
      }
    }
    this.changeAni();
  }

  private resetPos(): void {
    let deadLine: number = 0;
    switch (this.player.forward) {
      case Forward.Up:
      case Forward.Down:
        deadLine = this.player.nextPos.y * GameSet.girdHeight + GameSet.girdHeight / 2;
        this.node.y = deadLine;
        break;
      case Forward.Left:
      case Forward.Right:
        deadLine = this.player.nextPos.x * GameSet.girdWidth + GameSet.girdWidth / 2;
        this.node.x = deadLine;
        break;
    }
  }
}
